package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.CustomerDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.Customer;
import com.atguigu.jxc.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 杨林
 * @create 2023-01-28 23:28 星期六
 * description:
 */
@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    CustomerDao customerDao;

    /**
     * 客户列表分业 （名称模糊查询）
     * @param page
     * @param rows
     * @param customerName
     * @return
     */
    @Override
    public Map<String, Object> list(Integer page, Integer rows, String customerName) {
        HashMap<String, Object> map = new HashMap<>();
        page = page == 0 ? 1 : page;
        int offSet = (page - 1) * rows;
        List<Customer> customerList =  customerDao.getCustomer(offSet,rows,customerName);
        Integer total = customerDao.getCustomerTotal(customerName);
        map.put("rows",customerList);
        map.put("total",total);
        return map;
    }

    /**
     * 添加或修改用户
     * @param customer
     * @return
     */
    @Override
    public ServiceVO saveOrUpdate(Customer customer) {
        Integer customerId = customer.getCustomerId();
        if (customerId != null){
            //这是修改操作
            customerDao.update(customer);
        }else{
            //这是新增操作
            customerDao.insert(customer);
        }
        return new ServiceVO(100,"请求成功",null);
    }

    /**
     * 删除客户
     * @param ids
     * @return
     */
    @Override
    public ServiceVO delete(String ids) {
        String[] idArray = ids.split(",");
        customerDao.delete(idArray);
        return new ServiceVO(100,"请求成功",null);
    }

    /**
     * 客户下拉列表 (可模糊匹配客户名称)
     * @param q
     * @return
     */
    @Override
    public List<Customer> getComboboxList(String q) {

        List<Customer> customerList = customerDao.getComboboxList(q);

        return customerList;
    }
}
